<!-- 订单调整列表 -->
<template>
  <div class="base_price_audit">
    <dt-search ref="search" @reset="resetFun" :xlBtn="12">
      <span v-if="orderType == '退购' || orderType == '退签'">
        <Button type="primary" v-if="showExport1" @click="exportTable(0)">导出</Button>
        <Button type="primary" v-if="showExport2" @click="exportTable(1)" style="margin-right: 10px">
          全号导出
        </Button>
      </span>
    </dt-search>
    <Tabs v-model="curState" type="card" :animated="false" @on-click="onChangeTab">
      <TabPane label="待审核" name="0"></TabPane>
      <TabPane label="已通过" name="1"></TabPane>
      <TabPane label="已驳回" name="2"></TabPane>
    </Tabs>
    <dt-grid ref="table" page :onFetch="onFetch" />
    <process-modal ref="process" />
    <!--调整附件-->
    <adjust-enclosure ref="adjustEnclosure"></adjust-enclosure>
    <Modal v-model="detailsFlag" title="客户详情" width="80%" :styles="{ top: '5%' }" :footer-hide="true">
      <detail-page v-if="detailsFlag" :detailsParams="detailsParams"></detail-page>
    </Modal>
  </div>
</template>

<script>
  import searchOpt from '../opt/search'
  import Column from '../opt/columns'
  import returnPurchaseColumns from '../opt/returnPurchaseColumn.js'
  import returnNoteColumns from '../opt/returnNoteColumns.js'
  import auditMgr from '@/biz/flowApproval/processAudit.js'
  import processModal from '@/components/biz/audit/process-modal.vue'
  import adjustEnclosure from '@/views/casemanagement/salesManagement/components/adjustEnclosure.vue'
  import backMgr from '@/biz/sales/backOrder'
  import refundMgr from '@/biz/sales/refundOrder'
  import detailPage from '../../../casemanagement/allCustomersManagement/details.vue'
  import dt from '@dt/dt'
  let backIds = []

  export default {
    components: {
      processModal,
      adjustEnclosure,
      detailPage
    },
    props: ['orderType'],
    data() {
      return {
        detailsFlag: false,
        detailsParams: {},
        curState: '0',
        oldState: '0',
        formValidate: {
          orgId: [],
          housesId: ''
        },
        filterParams: {}
      }
    },
    computed: {
      showExport1() {
        if (this.orderType == '退购') {
          return this.unlocking('0206100102')
        } else if (this.orderType == '退签') {
          return this.unlocking('0206100202')
        }
      },
      showExport2() {
        if (this.orderType == '退购') {
          return this.unlocking('0206100103')
        } else if (this.orderType == '退签') {
          return this.unlocking('0206100203')
        }
      }
    },
    methods: {
      // 客户姓名链接到客户资料
      onLinkUser(row) {
        if (row.customerName == '-') {
          return false
        }
        this.detailsFlag = true
        this.detailsParams = {
          customerId: row.customerId, // 手机号唯一编码
          userCustomerId: row.userCustomerId, // 客户信息唯一编码
          buildingId: row.housesId,
          userId: row.userId,
          cityCode: row.cityId
        }
      },

      onChangeTab(val) {
        if (val == this.oldState) return false
        this.oldState = val
        this.init()
        this.$refs.table.clearFilter()
      },
      resetFun() {
        backIds = []
        dt.memory.remove('orderAdjustApproveManagement')
        this.formValidate = {
          orgId: [],
          housesId: ''
        }
        this.$refs.search.init(this.$refs.table, new searchOpt(this).options(), this.formValidate)
      },
      onFetch(data) {
        console.log(data, '111')
        data.backIds = backIds
        dt.memory.get('orderAdjustApproveManagement')?.UContainSelf !== undefined &&
          (data.containSelf = !dt.memory.get('orderAdjustApproveManagement'))

        if (this.orderType == '退购') {
          this.filterParams = {
            ...data,
            status: this.curState == 0 ? '3' : this.curState
          }

          this.filterParams.orgIds = this.filterParams.orgId
          delete this.filterParams.orgId
          return backMgr
            .getBackOrderList(this.filterParams)
            .then((res) => {
              return res
            })
            .catch((err) => {})
        } else if (this.orderType == '退签') {
          this.filterParams = {
            ...data,
            businessResult: this.curState == 0 ? '3' : this.curState
          }

          this.filterParams.orgIds = this.filterParams.orgId
          delete this.filterParams.orgId
          return refundMgr
            .getRefundOrderList(this.filterParams)
            .then((res) => {
              return res
            })
            .catch((err) => {})
        } else {
          this.filterParams = {
            ...data,
            auditType: this.orderType == '认购' ? 10 : 11,
            buttonType: this.curState
          }
          return auditMgr.list(this.filterParams).then((res) => {
            res &&
              res.list.forEach((item) => {
                item = Object.assign(item, item.auditData)
              })
            return res
          })
        }
      },
      init() {
        if (this.orderType == '退购') {
          this.$refs.table.init(this, new returnPurchaseColumns(this))
        } else if (this.orderType == '退签') {
          this.$refs.table.init(this, new returnNoteColumns(this))
        } else {
          this.$refs.table.init(this, new Column(this))
        }

        this.$refs.table.fetch()
      },

      // // 退购查看、审核
      detailFun(params) {
        console.log(params, 'ttttttt')
        let orderParams = {}
        let row = params[0]
        let type = params[1]
        switch (type) {
          case 'UQ':
            orderParams = {
              orderNum: row.backCode, // 单号
              businessId: row.backId, // 业务id
              buildingId: row.housesId,
              subscriptionId: '',
              returnNote: true
            }
            break
          case 'UR':
            orderParams = {
              orderNum: row.backCode, // 单号
              businessId: row.backId, // 业务id
              buildingId: row.housesId,
              subscriptionId: '',
              returnPurchase: true
            }
            break
          case 'QY':
            orderParams = {
              orderNum: row.contractCode, // 单号
              businessId: row.contractId, // 业务id
              buildingId: row.housesId,
              subscriptionId: '',
              returnNote: true
            }
            break
          case 'RG':
            orderParams = {
              orderNum: row.orderCode, // 单号
              businessId: row.subscriptionId, // 业务id
              buildingId: row.housesId,
              subscriptionId: '',
              returnPurchase: true
            }
            break
          default:
            break
        }
        this.linkOrderFun(orderParams)
      },

      auditFun(data) {
        console.log(data)
        data.title = '审核'
        data.flag = 'audit'
        this.$router.push({
          name: `${this.orderType == '认购' ? 'subApproveDetail' : 'contractApproveDetail'}`,
          query: {
            adjustmentId: data.adjustmentId,
            orderType: this.orderType,
            auditId: data.auditId,
            housesId: data.housesId,
            isflag: 'edit'
          }
        })
      },

      viewFun(data) {
        data.title = '查看'
        data.flag = 'check'
        this.$router.push({
          name: `${this.orderType == '认购' ? 'subApproveDetail' : 'contractApproveDetail'}`,
          query: {
            adjustmentId: data.adjustmentId,
            orderType: this.orderType,
            auditId: data.auditId,
            housesId: data.housesId,
            isflag: 'check'
          }
        })
      },
      //查看附件
      viewAccessory(data) {
        console.log(data, '查看附件')
        data.imageAttachList = data.imageAnnexes
        data.fileAttachList = data.fileAnnexes
        data.fromPage = 'approve'
        this.$refs.adjustEnclosure.show(data)
      },
      success() {
        this.$refs.table.fetch()
      },
      //导出
      exportTable(exportType) {
        let params = {
          ...this.filterParams
        }
        params.exportType = exportType
        console.log('zhk ~ file: list.vue:242 ~ exportTable ~ params:', params)
        if (this.orderType == '退签') {
          refundMgr.exportContractApproval(params).then()
        } else if (this.orderType == '退购') {
          backMgr.exportApprovalBack(params).then()
        }
      },
      processFun(data) {
        this.$refs.process.show({
          businessId: data.businessId,
          auditType: this.orderType == '认购' ? 10 : 11
        })
      }
    },
    mounted() {
      this.curState = this.$route.params.tabCheck || '0'
      console.log(this.orderType, 'orderType')
      backIds = this.$route.params.businessId || backIds
      this.init()
      this.$refs.search.init(this.$refs.table, new searchOpt(this).options(), this.formValidate)
    }
  }
</script>
<style lang="scss" scoped>
  .base_price_audit {
    padding: 20px;
  }
</style>
